package com.ocom.accesservice.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.service.*;
import com.ocom.accesservice.vo.area.GetAreaGroupListVo;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.constant.UserConstants;
import com.ocom.common.dto.AreaListDto;
import com.ocom.common.dto.MachineListDto;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.entity.access.EntityMjGroupInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.AreaVo;
import com.ocom.common.vo.DelAreaVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class AreaListController {
    Logger log = LoggerFactory.getLogger(AreaListController.class);
    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private GroupPerService groupPerService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private RegionInfoService regionInfoService;

    @Autowired
    private GroupInfoService groupInfoService;

    @Autowired
    private TimeInfoService timeInfoService;

    @Autowired
    private WhiteInfoService whiteInfoService;

    @Autowired
    PersonnelInfoService personnelInfoService;
    /**
     * 获取门禁区域信息
     * */
    @PostMapping(AccessServiceUrl.URL_GetAreaList)
    public Result  GetAreaList(@Validated @RequestBody AreaVo areaVo){
        //1、从门禁区域表mj_area_info查出所有com_id=companyID的areaNo(areaName)
        List<EntityMjAreaInfo> entityMjAreaInfos = areaInfoService.GetAreaName(areaVo,areaVo.getComId());
        PageInfo<EntityMjAreaInfo> entityMjAreaInfoPageInfo = new PageInfo<>(entityMjAreaInfos);

        //封装areaInfo
        ArrayList<AreaListDto> areaInfo = new ArrayList<>();
        for (EntityMjAreaInfo entityMjAreaInfo :entityMjAreaInfos) {
            AreaListDto infoDto = new AreaListDto();
            infoDto.setAreaId(entityMjAreaInfo.getAreaId());
            infoDto.setComId(entityMjAreaInfo.getComId());
            infoDto.setAreaName(entityMjAreaInfo.getAreaName());
            infoDto.setRegionId(entityMjAreaInfo.getRegionId());
            infoDto.setUserName(entityMjAreaInfo.getUserName());
            infoDto.setIndoorState(entityMjAreaInfo.getIndoorState());
            infoDto.setOutdoorState(entityMjAreaInfo.getOutdoorState());
            //3.根据片区id获取片区名称
            try {
                String regionName = regionInfoService.GetRegionName(entityMjAreaInfo.getRegionId(), areaVo.getComId());
                infoDto.setRegionName(regionName);
            }catch (Exception exception){
                exception.printStackTrace();
            }
            //2、对每个areaNo：
            //a)从人员门禁权限表mj_group_per查出count (p_id)
            try {
                Integer userNumber = groupPerService.CountPid(entityMjAreaInfo.getAreaId(), areaVo.getComId());
                infoDto.setUserNumber(userNumber);
            }catch (Exception exception){
                exception.printStackTrace();
            }
            //获取人脸数量

            try {
                Integer faceNumber = personnelInfoService.getFaceNumber(areaVo.getComId(),entityMjAreaInfo.getAreaId());
                infoDto.setFaceNumber(faceNumber);
            }catch (Exception exception){
                exception.printStackTrace();
            }

            

            //4.根据区域id获取设备数量
            try {
                Integer machineNumber = deviceInfoService.CountMachineNumber(entityMjAreaInfo.getAreaId(), areaVo.getComId());
                infoDto.setMachineNumber(machineNumber);
            }catch (Exception exception){
                exception.printStackTrace();
            }
            //b)从门禁设备表mj_device_info查出所有area_id=areaNo的记录
            try {
                List<MachineListDto> deviceInfo = deviceInfoService.getDeviceInfo(entityMjAreaInfo.getAreaId(),areaVo.getComId());
                if (StringUtils.isNotNull(deviceInfo)&& !deviceInfo.equals(0)) {
                    infoDto.setMachineList(deviceInfo);
                }
            }catch (Exception exception){
                exception.printStackTrace();
            }
            areaInfo.add(infoDto);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("total", entityMjAreaInfoPageInfo.getTotal());
        map.put("list", areaInfo);
        return ResultUtil.success(map);
    }

    /**
     * 移除区域
     * */
    @PostMapping(AccessServiceUrl.URL_RemoveArea)
    public Result RemoveArea(@Validated @RequestBody DelAreaVo delAreaVo){
        if (delAreaVo.getComId() <= 0 || delAreaVo.getAreaId() <= 0){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        if ((UserConstants.NOT_UNIQUE.equals(deviceInfoService.checkDeviceInfoUnique(delAreaVo.getComId() ,delAreaVo.getAreaId())))){
            return ResultUtil.error(ResultEnum.Err_3001);
        }
        try {
            groupPerService.RemoveArea(delAreaVo,delAreaVo.getComId());
            groupInfoService.RemoveArea(delAreaVo,delAreaVo.getComId());
            areaInfoService.RemoveArea(delAreaVo,delAreaVo.getComId());
            timeInfoService.RemoveArea(delAreaVo,delAreaVo.getComId());
            whiteInfoService.RemoveArea(delAreaVo,delAreaVo.getComId());
        }catch (Exception exception){
            log.error("{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }
        return ResultUtil.success();
    }

    /**
     * 区域权限列表 （getAreaGroupList）
     * */
    @PostMapping(AccessServiceUrl.URL_GetAreaGroupList)
    public Result  GetAreaGroupList(@Validated @RequestBody GetAreaGroupListVo getAreaGroupListVo){
        log.info("区域权限列表请求参数【{}】",getAreaGroupListVo);
        List<Map> list = new ArrayList<>();
        try {
            List<EntityMjAreaInfo> entityMjAreaInfos = areaInfoService.GetAreaGroupList(getAreaGroupListVo.getCompanyId(), getAreaGroupListVo.getAreaId(), getAreaGroupListVo.getRange());
            log.info("entityMjAreaInfos{{}}",entityMjAreaInfos);
            for (EntityMjAreaInfo entityMjAreaInfo: entityMjAreaInfos) {
                Map<String, Object> map = new HashMap<>();
                map.put("areaId",entityMjAreaInfo.getAreaId());
                map.put("areaName",entityMjAreaInfo.getAreaName());
                List<Map> roleList = new ArrayList<>();
                List<EntityMjGroupInfo> groupList = groupInfoService.getGroupList(getAreaGroupListVo.getCompanyId());
                log.info("groupList{{}}",groupList);
                for (EntityMjGroupInfo groupInfo: groupList) {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("roleId",groupInfo.getGroupNo());
                    map1.put("groupName",groupInfo.getGroupName());
                    roleList.add(map1);
                }
                Map<String, Object> map1 = new HashMap<>();
                map1.put("roleId",0);
                map1.put("groupName","禁止" );
                roleList.add(map1);
                map.put("roleList",roleList);
                list.add(map);
            }
        }catch (Exception exception){
            log.error("获取区域失败");
        }
        log.info("list{{}}",list);
        return ResultUtil.success(list);
    }


}
